package com.netease.utils;

import com.alibaba.fastjson2.JSONArray;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * 去除非元音开头单词的元音
     * @return
     */
     public static String removeVowels(String str){
         String[] splits = str.split("\\|");
         StringBuilder sb = new StringBuilder();
         for (int i = 0; i < splits.length; i++) {
             //判断是否是词组
             if (!StringUtils.isPhrase(splits[i])){
                 //判断单词长度是否超过5
                 if (isStringLenGreaterThan5(splits[i])){
                     if (splits[i].startsWith("a") || splits[i].startsWith("e") || splits[i].startsWith("i") || splits[i].startsWith("o") || splits[i].startsWith("u")) {
                         String prefix = splits[i].substring(0, 1);
                         String suffix = splits[i].substring(1);
                         sb.append(prefix + suffix.replaceAll("a|e|i|o|u",""));
                     }else{
                         sb.append(splits[i].replaceAll("a|e|i|o|u",""));
                     }
                 }else{
                     sb.append(splits[i]);
                 }
             }else{
                 String[] words = splits[i].split(" ");
                 ArrayList<String> wordList = new ArrayList<>(Arrays.asList(words));
                 wordList.removeIf(e -> e.matches("of|the|and|on|or|to|for|in|a"));

                 for (int i1 = 0; i1 < wordList.size(); i1++) {
                     //判断字符串长度是否大于5
                     if(isStringLenGreaterThan5(wordList.get(i1))){
                         if (wordList.get(i1).startsWith("a") || wordList.get(i1).startsWith("e") || wordList.get(i1).startsWith("i") || wordList.get(i1).startsWith("o") || wordList.get(i1).startsWith("u")) {
                             String prefix = wordList.get(i1).substring(0, 1);
                             String suffix = wordList.get(i1).substring(1);
                             sb.append(prefix + suffix.replaceAll("a|e|i|o|u",""));
                         }else{
                             sb.append(wordList.get(i1).replaceAll("a|e|i|o|u",""));
                         }
                     }else{
                         //长度小于5不用去除元音
                         sb.append(wordList.get(i1));
                     }
                     //拼接空格
                     if (i1 < wordList.size() - 1){
                         sb.append(" ");
                     }
                 }
             }
             if (i < splits.length - 1){
                 sb.append("|");
             }
         }
         return sb.toString();
     }

     /**
      * @param str:
      * @return boolean
      * @author aochong
      * @description 判断字符串是否是英文
      * @date 2023/4/12 11:10
      */
     public static boolean isEnglish(String str){
         return str.matches("^[a-zA-Z]*");
     }

     /**
      * @param str:
      * @return boolean
      * @author aochong
      * @description 判断字符串是否是中文
      * @date 2023/4/12 11:11
      */
     public static boolean isChinese(String str) {
         String regex = "[\\u4e00-\\u9fa5]+";
         System.out.println(regex);
         Pattern pattern = Pattern.compile(regex);
         Matcher matcher = pattern.matcher(str);
         return matcher.find();
     }

     /**
      * @param str:
      * @return boolean
      * @author aochong
      * @description 判断字符串长度是否大于5
      * @date 2023/3/13 20:34
      */
      public static boolean isStringLenGreaterThan5(String str){
         if (str.length() > 5) {
             return true;
         }
         return false;
      }

      public static void isBeginningVowels(String word, StringBuilder sb) {
          if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u")) {
              String prefix = word.substring(0, 1);
              String suffix = word.substring(1);
              sb.append(prefix + suffix.replaceAll("a|e|i|o|u",""));
          }
      }

     public static String jsonArray2String(JSONArray jsonArray,StringBuilder sb){
         String oneWord = sb.toString();
         //拼接字符串
         for (int i1 = 0; i1 < jsonArray.size(); i1++) {
             if(!oneWord.equals(jsonArray.get(i1))){
                sb.append("|").append(jsonArray.get(i1));
             }
         }
         return sb.toString();
     }

     public static String sortWord(String word){
         String[] split = word.split(" ");
         StringBuilder sb = new StringBuilder();
         ArrayList<String> list = new ArrayList<>();
         for (int i = 0; i < split.length; i++) {
             list.add(split[i]);
         }

         Collections.sort(list, new Comparator<String>() {
             @Override
             public int compare(String s1, String s2) {
                 if (s1.length() > s2.length()){
                     return -1;
                 }else if(s1.length() == s2.length()){
                     return 0;
                 }else{
                     return 1;
                 }
             }
         });

         for (int i = 0; i < list.size(); i++) {
             sb.append(list.get(i).trim());
             if (i < list.size() - 1) {
                 sb.append(" ");
             }
         }

         return sb.toString();
     }

    /**
     * 判断字符串是否是词组
     * text -> false
     * test data -> true
     * @param word
     * @return
     */
     public static boolean isPhrase(String word) {
         if (word.split(" ").length <= 1){
             return false;
         }
         return true;
     }

     /**
      * @param str:
      * @return boolean
      * @author aochong
      * @description 判断字符串不是空白的
      * @date 2023/6/27 10:40
      */
     public static boolean isNotBlank(String str){
         if (str == null) {
             return false;
         }

         if ("null".equals(str)){
             return false;
         }

         if ("".equals(str)){
             return false;
         }

         return true;
     }

    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println(isChinese(str));
        System.out.println(isEnglish(str));
    }

}
